//
//  HomePublicController.m
//  PropertyMananger
//
//  Created by bh on 17/3/8.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "HomePublicController.h"
#import "HomePublicCell.h"
#import "PublicDetailController.h"
#import "MyPublicModel.h"

@interface HomePublicController ()<RefreshStatusDelegate>
/*我的-公共报修数据*/
@property (nonatomic,strong) NSMutableArray * myRepairArray;

@property (nonatomic,copy) NSString * selectedTime;//选择的的时间
@end

@implementation HomePublicController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 44, BHscreenW, BHscreenH - 64 - 44) isGroup:NO];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = BHGrayColor;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //暂无数据暂位图
    if (self.myRepairArray.count == 0) {
        
        UIView * lineView = [[UIView alloc]init];
        [self.tableView addSubview:lineView];
        lineView.backgroundColor = [BHColor(153, 153, 153)colorWithAlphaComponent:0.2];
        self.tableView.backgroundColor = [UIColor whiteColor];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(44);
            make.size.mas_equalTo(CGSizeMake(BHscreenW, 15));
        }];
        
    }
    
    self.myRepairArray = [[NSMutableArray alloc]init];
    
    [self createRefresh];
    
    [self createPlaceHolder];
    
    [self postMyOrderRepairData];
}

#pragma mark - 创建刷新UI
- (void)createRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.nextPage = 1;
        
        self.isDown = YES;
        //获取数据
        [self postMyOrderRepairData];
        
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.nextPage += 1;
        
        self.isDown = NO;
        //获取数据
        [self postMyOrderRepairData];
        
        //    [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    }];
    
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.myRepairArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HomePublicCell * cell = [HomePublicCell cellWithTableView:tableView];
    
    MyPublicModel * model = self.myRepairArray[indexPath.section];
    
    cell.orderCodeLabel.text = [NSString stringWithFormat:@"订单编号: %@",model.RepairCode];
    cell.orderProgressLable.text = model.ProcessState;
    cell.repairLabel.text = model.CategoryName;
    cell.placeLabel.text = model.RepairAddr;
    cell.repairManLabel.text = model.LinkMan;
    
    if ([model.ProcessState isEqualToString:@"10"]) {
        
        cell.orderProgressLable.text = @"待处理";
        cell.orderProgressLable.textColor = [UIColor redColor];
        cell.orderProgressLable.layer.borderColor = [UIColor redColor].CGColor;
        
    }else if ([model.ProcessState isEqualToString:@"30"]){
        
        cell.orderProgressLable.text = @"处理中";
        cell.orderProgressLable.textColor = BHColor(215, 152, 2);
        cell.orderProgressLable.layer.borderColor = BHColor(215, 152, 2).CGColor;
        
    }else if ([model.ProcessState isEqualToString:@"100"]){
        cell.orderProgressLable.text = @"已处理";
        cell.orderProgressLable.textColor = BHColor(17, 177, 31);
        cell.orderProgressLable.layer.borderColor = BHColor(17, 177, 31).CGColor;
    }else if ([model.ProcessState isEqualToString:@"40"]){
        cell.orderProgressLable.text = @"已回访";
        cell.orderProgressLable.textColor = BHColor(1, 181, 151);
        cell.orderProgressLable.layer.borderColor = BHColor(1, 181, 151).CGColor;
    }else if ([model.ProcessState isEqualToString:@"50"]){
        cell.orderProgressLable.text = @"完成";
        cell.orderProgressLable.textColor = [UIColor redColor];
        cell.orderProgressLable.layer.borderColor =[UIColor redColor].CGColor;
    }
    
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PublicDetailController * detailVC = [[PublicDetailController alloc]init];
    detailVC.model = self.myRepairArray[indexPath.section];
    detailVC.delegate = self;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}
- (void)didRefreshDataToRepair{
    
    self.isDown = YES;
    [self postMyOrderRepairData];
    DLog(@"重新请求数据");
}
#pragma mark - 数据请求
- (void)postMyOrderRepairData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    NSDictionary * parms =  @{
                              @"CommunityID":userModel.CommunityID,
                              @"WorkerId":userModel.WorkerID,
                              @"RepairType":@"10",
                              @"QueryType":@"1",
                              @"Time":@"",
                              @"Page":[NSString stringWithFormat:@"%ld",self.nextPage],
                              @"json":@"\{\"jsonEntity\":\{},\"action\":\"repairlist\",\"version\":\"V1.0\"}"
                              };
    DLog(@"提交的参数 === %@",parms);
    
    [MineRequest postMyOrderRepairSuccess:^(id data) {
        [MBProgressHUD hideHUDForView:self.view];
        
        if (self.isDown == YES) {
            
            [self.myRepairArray removeAllObjects];
            self.isDown = NO;
        }
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"我的工单报修的数据 == %@",array);
        DLog(@" == %@",str);
        
        for (NSDictionary * dict in array) {
            
            MyPublicModel * model = [[MyPublicModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            
            [self.myRepairArray addObject:model];
            // DLog(@"%@",model.LinkMan);
        }
        if (self.myRepairArray.count != 0){
            
            self.placehoderImg.hidden = YES;
            self.titleLabel.hidden = YES;
            
        }else{
            
            self.placehoderImg.hidden = NO;
            self.titleLabel.hidden = NO;
            
        }
        
        if (self.myRepairArray.count <= 3) {
            
            self.tableView.mj_footer.hidden = YES;
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
    } Fail:^(NSString *errorDes) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [MBProgressHUD hideHUD];
        
    } param:parms ];
    
}
#pragma mark - 暂未图
- (void)createPlaceHolder
{
    self.placehoderImg = [[UIImageView alloc]init];
    [self.tableView addSubview:self.placehoderImg];
    self.placehoderImg.image = [UIImage imageNamed:@"ico_repair_no"];
    
    [self.placehoderImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.tableView.mas_bottom).with.offset(100);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.tableView addSubview:self.titleLabel];
    self.titleLabel.text = @"暂无公共报修单";
    
    self.titleLabel.textColor= BHColor(153, 153, 153);
    self.titleLabel.font = BHFont(15);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.placehoderImg.mas_bottom).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(120, 25));
    }];
    
}


@end
